import { Module, ValidationPipe } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { InstrumentModule } from './instrument/instrument.module';
import { BookingModule } from './booking/booking.module';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { join } from 'path';
import { AppFilter } from './app.filter';
import { AppInterceptor } from './app.interceptor';

@Module({
  imports: [
    InstrumentModule,
    BookingModule,
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory(config: ConfigService) {
        return {
          type: config.get('DB_TYPE'),
          host: config.get('DB_HOST'),
          port: config.get('DB_PORT'),
          username: config.get('DB_USERNAME'),
          password: config.get('DB_PASSWORD'),
          database: config.get('DB_NAME'),
          entities: [join(__dirname, '/**/*.entity{.ts,.js}')],
          synchronize: true,
          // logging: true,
        } as TypeOrmModuleOptions;
      },
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'APP_FILTER',
      useClass: AppFilter,
    },
    {
      provide: 'APP_INTERCEPTOR',
      useClass: AppInterceptor,
    },
    {
      provide: 'APP_PIPE',
      useFactory: () => {
        return new ValidationPipe({
          transform: true, // 转换输入类型
          whitelist: true, // 过滤未在 DTO 中定义的字段
          // forbidNonWhitelisted: true, // 拒绝包含不在 DTO 中的字段
        });
      },
    },
  ],
})
export class AppModule {}
